from datetime import datetime
from typing import List

import pandas as pd

from app.models.po.luzha.luzha_predict_po import LuzhaPredictPO
from app.services.business.hanfan import luzha_biz_service
from app.services.chart.Line import Line
from app.utils import date_util, simple_util, string_util
from datetime import timedelta

class LuzhaComponent(Line):
    """
    炉渣数据表
    """

    def generate(self, start, end):

        if string_util.is_empty(start) or string_util.is_empty(end):
            base = date_util.get_start_of_hour(datetime.now())
            start = base-timedelta(days=1)
            end = base

        yAxis_array = [
            {
                "name": "TiO2",
                "type": 'value',
                "position": 'left',
                "offset":0
            },
            {
                "name": "Mg_Al",
                "type": 'value',
                "position": 'left',
                "offset": 30,
                "min":0,
                "max":1
            },
            {
                "name": "R2",
                "type": 'value',
                "position": 'left',
                "offset": 60,
                "min":0,
                "max":4
            },

        ]

        records: List[LuzhaPredictPO] = luzha_biz_service.get_luzha_data(start, end)
        data_x = []
        data_y_array = [dict(name="", y=[], yAxisIndex=0,type="line") for i in range(3)]
        for record in records:
            data_x.append(date_util.date_time_to_str(record.DATE_TIME, "%H:%M"))
            data_y_array[0]['name'] = "TiO2"
            data_y_array[0]['y'].append(record.CG_LT_GL_GL04_Zha_TiO2)
            data_y_array[0]['yAxisIndex'] = 0

            data_y_array[1]['name'] = "Mg_Al"
            data_y_array[1]['y'].append(record.CG_LT_GL_GL04_Zha_Mg_Al)
            data_y_array[1]['yAxisIndex'] = 1


            data_y_array[2]['name'] = "R2"
            data_y_array[2]['y'].append(record.CG_LT_GL_GL04_Zha_R)
            data_y_array[2]['yAxisIndex'] = 2

        return dict(x=data_x,
                    tooltip={
                        "trigger": 'axis'
                    },
                    y_array=data_y_array, desc="炉渣成分变化曲线", multiSerie=True,
                    yAxis_array=yAxis_array)
